package com.miukoo.aikao.metadata.controller.v1;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.miukoo.aikao.core.dtos.ApiResult;
import com.miukoo.aikao.core.dtos.BaseIdReqDto;
import com.miukoo.aikao.core.dtos.BaseIdsReqDto;
import com.miukoo.aikao.core.dtos.PageReqDto;
import com.miukoo.aikao.core.utils.ApiUtils;
import com.miukoo.aikao.core.utils.BeanMapperUtils;
import com.miukoo.aikao.metadata.dto.SysDepartDto;
import com.miukoo.aikao.metadata.dto.request.DepartSortReqDto;
import com.miukoo.aikao.metadata.dto.response.SysDepartTreeDto;
import com.miukoo.aikao.metadata.pojos.SysDepart;
import com.miukoo.aikao.metadata.service.SysDepartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
* <p>
* 部门信息控制器
* </p>
*
* @author 聪明笨狗
* @since 2020-09-02 17:25
*/
@Api(tags={"部门信息"})
@RestController
@RequestMapping("/exam/api/sys/depart")
public class SysDepartController {

    @Autowired
    private SysDepartService baseService;

    /**
    * 添加或修改
    * @param reqDto
    * @return
    */
    @ApiOperation(value = "添加或修改")
    @RequestMapping(value = "/save", method = { RequestMethod.POST})
    public ApiResult save(@RequestBody SysDepartDto reqDto) {
        baseService.save(reqDto);
        return ApiUtils.success();
    }

    /**
    * 批量删除
    * @param reqDto
    * @return
    */
    @ApiOperation(value = "批量删除")
    @RequestMapping(value = "/delete", method = { RequestMethod.POST})
    public ApiResult edit(@RequestBody BaseIdsReqDto reqDto) {
        //根据ID删除
        baseService.removeByIds(reqDto.getIds());
        return ApiUtils.success();
    }

    /**
    * 查找详情
    * @param reqDto
    * @return
    */
    @ApiOperation(value = "查找详情")
    @RequestMapping(value = "/detail", method = { RequestMethod.POST})
    public ApiResult<SysDepartDto> find(@RequestBody BaseIdReqDto reqDto) {
        SysDepart entity = baseService.getById(reqDto.getId());
        SysDepartDto dto = new SysDepartDto();
        BeanUtils.copyProperties(entity, dto);
        return ApiUtils.success(dto);
    }

    /**
    * 分页查找
    * @param reqDto
    * @return
    */
    @ApiOperation(value = "分页查找")
    @RequestMapping(value = "/paging", method = { RequestMethod.POST})
    public ApiResult<IPage<SysDepartTreeDto>> paging(@RequestBody PageReqDto<SysDepartDto> reqDto) {

        //分页查询并转换
        IPage<SysDepartTreeDto> page = baseService.paging(reqDto);

        return ApiUtils.success(page);
    }

    /**
     * 查找列表，每次最多返回200条数据
     * @param reqDto
     * @return
     */
    @ApiOperation(value = "查找列表")
    @RequestMapping(value = "/list", method = { RequestMethod.POST})
    public ApiResult<List<SysDepartDto>> list(@RequestBody SysDepartDto reqDto) {

        //分页查询并转换
        QueryWrapper<SysDepart> wrapper = new QueryWrapper<>();

        //转换并返回
        List<SysDepart> list = baseService.list(wrapper);

        //转换数据
        List<SysDepartDto> dtoList = BeanMapperUtils.mapList(list, SysDepartDto.class);

        return ApiUtils.success(dtoList);
    }


    /**
     * 树列表
     * @return
     */
    @ApiOperation(value = "树列表")
    @RequestMapping(value = "/tree", method = { RequestMethod.POST})
    public ApiResult<List<SysDepartTreeDto>> tree() {
        List<SysDepartTreeDto> dtoList = baseService.findTree();
        return ApiUtils.success(dtoList);
    }


    /**
     * 分类排序
     * @param reqDto
     * @return
     */
    @ApiOperation(value = "分类排序")
    @RequestMapping(value = "/sort", method = { RequestMethod.POST})
    public ApiResult sort(@RequestBody DepartSortReqDto reqDto) {
        baseService.sort(reqDto.getId(), reqDto.getSort());
        return ApiUtils.success();
    }
}
